package com.fc.controller;


import com.fc.context.BaseContext;
import com.fc.dto.LogisticsCompleteDTO;
import com.fc.dto.LogisticsDTO;
import com.fc.dto.pageQuery.LogisticsPageQueryDTO;
import com.fc.entity.Logistics;
import com.fc.result.PageResult;
import com.fc.result.Result;
import com.fc.service.ILogisticsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Api(tags = "物流")
@RestController
@RequestMapping("/logistics")
public class LogisticsController {

    @Autowired
    private ILogisticsService logisticsService;


    /**
     * 物流信息填写和提交
     *
     * @param logisticsDTO
     * @return
     */
    @ApiOperation("物流信息填写和提交")
    @PostMapping
    public Result save(@RequestBody LogisticsDTO logisticsDTO) {
        Long logisticsId = logisticsService.commit(logisticsDTO);
        return Result.success(logisticsId);
    }

    /**
     * 物流信息查看
     *
     * @return
     */
    @ApiOperation("根据id查询物流信息")
    @GetMapping("/{id}")
    public Result<Logistics> select(@PathVariable Integer id) {
        Logistics logistics = logisticsService.getById(id);
        return Result.success(logistics);
    }

    /**
     * 物流信息修改
     *
     * @return
     */
    @ApiOperation("根据id修改物流信息")
    @PutMapping
    public Result update(@RequestBody LogisticsDTO logisticsDTO) {
        logisticsService.updateLogistics(logisticsDTO);
        return Result.success();
    }

    /**
     * 物流运输完成
     *
     * @return
     */
    @ApiOperation("物流运输完成")
    @PutMapping("/complete")
    public Result complete(@RequestBody LogisticsCompleteDTO logisticsCompleteDTO) {
        logisticsService.complete(logisticsCompleteDTO);
        return Result.success();
    }

    /**
     * 查询物流信息列表
     *
     * @return
     */
    @PreAuthorize("hasRole('ADMIN')")
    @ApiOperation("查询物流信息列表")
    @PostMapping("/page")
    public Result<PageResult> page(@RequestBody LogisticsPageQueryDTO logisticsPageQueryDTO) {
        PageResult pageResult = logisticsService.listLogistics(logisticsPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 司机查询查询自己物流信息列表
     *
     * @return
     */
    @ApiOperation("司机查询查询自己物流信息列表")
    @PostMapping("/list")
    public Result<PageResult> list(@RequestBody LogisticsPageQueryDTO logisticsPageQueryDTO) {
        logisticsPageQueryDTO.setDriverId(BaseContext.getCurrentId());
        PageResult pageResult = logisticsService.listLogistics(logisticsPageQueryDTO);
        return Result.success(pageResult);
    }

}
